﻿using Blm.PgsqlDriver.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Inner;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;
using VisionCloud.Domain.UnitOfWork;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsTaskDotCloudtRepository : BaseRepository<MsTaskDotCloud>, IMsTaskDotCloudRepository
    {
        public IUnitOfWork UnitOfWork { get; set; }

        /// <summary>
        /// 批量添加或更新点云文件数据
        /// </summary>
        /// <param name="model"></param>
        public async ValueTask<List<MsTaskDotCloud>> AddorUpdateDotCloud(DotCloudUploadRequestModel model)
        {
            List<MsTaskDotCloud> list = new List<MsTaskDotCloud>();
            if (model == null || model.DotCloudList == null || model.DotCloudList.Count == 0)
                return list;

            //更新点云列表
            List<MsTaskDotCloud> updateDotCloudList = new List<MsTaskDotCloud>();
            //新增点云列表
            List<MsTaskDotCloud> addDotCloudList = new List<MsTaskDotCloud>();
            foreach (var item in model.DotCloudList)
            {
                var dotCloud = this.FirstOrDefault(p => p.FileName.Equals(item.FileName)
                && p.ProjectId.Equals(item.ProjectId)
                && p.TaskId.Equals(p.TaskId)
                && p.FileType.Equals(p.FileType)
                && p.ProjectStage.Equals(item.ProjectStage));
                if (dotCloud == null)
                {
                    addDotCloudList.Add(new MsTaskDotCloud
                    {
                        FileId = Guid.NewGuid(),
                        IsDeleted = false,
                        DotCloudFilePath = item.DotCloudFilePath,
                        FileName = item.FileName,
                        ProjectId = item.ProjectId,
                        TaskId = item.TaskId,
                        ProjectStage = item.ProjectStage,
                        CreatedDate = DateTime.Now,
                        FileType = item.FileType,
                        IsRead = false,
                        Site = item.Site?.Split(".")[0],
                        WallSpace = item.WallSpace?.Split(".")[0],
                        DotCloudZipPath = null,
                        TaskTypeName = item.TaskType,
                        ComputeStatus = 1
                    });
                }
                else
                {
                    dotCloud.ComputeStatus = 1;
                    dotCloud.ComputeMessage = null;
                    dotCloud.LastUpdatedDate = DateTime.Now;
                    dotCloud.DotCloudFilePath = item.DotCloudFilePath;
                    updateDotCloudList.Add(dotCloud);
                }
            }

            if (addDotCloudList.Count > 0)
                this.AddRange(addDotCloudList);

            if (updateDotCloudList.Count > 0)
            {
                foreach (var dt in updateDotCloudList)
                {
                    dt.IsDeleted = false;
                    dt.IsRead = false;
                    this.Update(dt, y => new { y.DotCloudFilePath, y.IsRead, y.IsDeleted, y.LastUpdatedDate, y.ComputeStatus, y.ComputeMessage });
                }
            }

            var count = await UnitOfWork.SaveChangesAsync();
            if (count > 0)
            {
                list.AddRange(addDotCloudList);
                list.AddRange(updateDotCloudList);
            }
            return list;
        }

        /// <summary>
        /// 获取待测量的只采点云
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async ValueTask<List<ComputeDotCloudDto>> GetComputeDotClouds()
        {
            string sql = "select * from vw_task_compute_dotcloud";
            var list = await PgSqlHelper.QueryListAsync<ComputeDotCloudDto>(CommandType.Text, sql);
            return list;
        }
    }
}